public with sharing class LoginLegalAdvise {
    public boolean readonly{get;set;}
    public user u{get;set;}
    public string getmessage{get;set;}
    public boolean showsave{get;set;}
   public pagereference logout(){
        PageReference ref=null;
        ref=new PageReference('/secur/logout.jsp'); 
        ref.setRedirect(true);
        return ref;
    }
    
    public pagereference accept(){
        u.logindatehidden__c=date.today().tostartofmonth();
        update u;
        PageReference ref=null;
        ref=new PageReference('/home/home.jsp'); 
        ref.setRedirect(true);
        return ref;
    }
    
    
    
    public pagereference run(){
        u=[select LoginDateHidden__c,id from user where id=:userinfo.getuserid()];
        Profile p  = [select id from profile where name=: 'System Administrator' or name=:'系统管理员'];
        if(u.LoginDateHidden__c!=date.today().tostartofmonth()|| userinfo.getProfileId()==p.id){
        showsave=false;
        readonly=true;
       
        list<StoreLoginMessage__c> slm=[select name,message__c from StoreLoginMessage__c order by name DESC];
        if(slm.size()>0){
            getmessage=slm[0].message__c;
        }
        if(p!=null){
        if(userinfo.getProfileId()==p.id){
            showsave=true;
            readonly=false;
        }
        }
        return null;
        }else{
        PageReference ref=null;
        ref=new PageReference('/home/home.jsp'); 
        ref.setRedirect(true);
        return ref;
        }
    }
    
    public void savemessage(){
        list<StoreLoginMessage__c> slm=[select name,message__c from StoreLoginMessage__c order by name DESC];
        if(slm.size()>0){
        if(getmessage!=slm[0].message__c){
            StoreLoginMessage__c s=new StoreLoginMessage__c(message__c=getmessage);
            insert s;
        }
        }else{
            StoreLoginMessage__c s=new StoreLoginMessage__c(message__c=getmessage);
            insert s;
        }
    }
    

    static testMethod void testTrigger() {
         String rtid;
        Profile p  = [select id from profile where name=: 'System Administrator' or name=:'系统管理员'];       

         User u3 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new2@testorg1.com');
        insert u3;
        User u2 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new@testorg1.com',managerid=u3.id);
        insert u2;
        
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com',managerid=u2.id);
        insert u1;

        
        system.runas(u1){
            LoginLegalAdvise  l=new LoginLegalAdvise();
            l.run();
            l.getmessage='aaa';
            l.savemessage();
            l.accept();
            
            LoginLegalAdvise  l1=new LoginLegalAdvise();
            l1.run();
            l1.logout();
        }
        
     }

}